/*
 * Copyright 2007 Kasper B. Graversen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.job.util;

import com.job.entity.WriteBean;

import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.constraint.UniqueHashCode;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvBeanWriter;
import org.supercsv.io.ICsvBeanWriter;
import org.supercsv.prefs.CsvPreference;

import java.io.FileWriter;
import java.util.Arrays;
import java.util.List;

/**
 * Examples of writing CSV files.
 */
public class Writing {

    public static void main(String[] args) throws Exception {
        writeWithCsvBeanWriter();
    }

    /**
     * Sets up the processors used for the examples. There are 10 CSV columns, so 10 processors are defined. All values
     * are converted to Strings before writing (there's no need to convert them), and null values will be written as
     * empty columns (no need to convert them to "").
     *
     * @return the cell processors
     */
    private static CellProcessor[] getProcessors() {

        final CellProcessor[] processors = new CellProcessor[]{new UniqueHashCode(), // customerNo (must be unique)
                new NotNull(), // firstName
                new NotNull() // lastName
        };

        return processors;
    }

    /**
     * An example of writing using CsvBeanWriter.
     */
    private static void writeWithCsvBeanWriter() throws Exception {

        // create the customer beans
        WriteBean john = new WriteBean();
        john.setCustomerNo("1");
        john.setFirstName("John");
        john.setLastName("Dunbar");

        WriteBean bob = new WriteBean();

        bob.setCustomerNo("2");
        bob.setFirstName("Bob");
        bob.setLastName("Down");


        final List<WriteBean> customers = Arrays.asList(john, bob);

        ICsvBeanWriter beanWriter = null;
        try {
            beanWriter = new CsvBeanWriter(new FileWriter("target/writeWithCsvBeanWriter.csv"),
                    CsvPreference.STANDARD_PREFERENCE);

            // the header elements are used to map the bean values to each column (names must match)
            final String[] header = new String[]{"customerNo", "firstName", "lastName"};
            final CellProcessor[] processors = getProcessors();

            // write the header
            beanWriter.writeHeader(header);

            // write the beans
            for (final WriteBean customer : customers) {
                beanWriter.write(customer, header, processors);
            }

        } finally {
            if (beanWriter != null) {
                beanWriter.close();
            }
        }
    }
}
